$(function(){
    $('#export').live('click',function(){
       var obj = $(this);
       var customer_id = obj.attr('customer_id');
       $.ajax({
            url: 'view/export/customer.php',
            type: "POST",
            async: true,                
            data: {
                customer_id:customer_id                   
            },
            success: function(data) {                                
            }
        });
    });
    $("input#date_giao").datepicker({
        dateFormat: 'dd-mm-yy',
        altFormat: 'yy-mm-dd',      
        minDate: 0
    });
    $("input#date_tra").datepicker({
        beforeShow: function() {

            var $toDateInput = $("input#date_tra");
            var fromDate = $("input#date_giao").datepicker("getDate");
            var toDate = $toDateInput.datepicker("getDate");
            var afterFromDate;

            if (fromDate) {
                if (!toDate || (toDate <= fromDate)) {
                    afterFromDate = new Date(fromDate.toUTCString());
                    afterFromDate.setDate(afterFromDate.getDate() + 1);
                    $toDateInput.datepicker("setDate", afterFromDate);
                }
                $toDateInput.datepicker("option", "minDate", fromDate);
            }
        },
        dateFormat: 'dd-mm-yy',
        altFormat: 'yy-mm-dd',  
        minDate: 0

    });
   $('#btnSaveCustomer').click(function(){
       if($.trim($('#customer_name').val())=='' || $.trim($('#phone').val())==''){
           $('#error_save_customer').fadeIn();
           scrollToTop();
           return false;
       }else{           
           var count = 0;
           $('#myModal1 input.input-small').each(function(){
               if($(this).val()=='') count++;               
           });
           if(count>12){
               $('#warning_sodo').fadeIn();
               scrollToTop();
           }
           savecustomer();
           $('#error_save_customer').fadeOut();
       }
   });
   $('#btnSaveCustomerEdit').live('click',function(){
       if($.trim($('#customer_name').val())=='' || $.trim($('#phone').val())==''){
           $('#error_save_customer').fadeIn();           
           return false;
       }else{           
           var count = 0;
           $('#myModal1 input.input-small').each(function(){
               if($(this).val()=='') count++;               
           });
           if(count>12){
               $('#warning_sodo').fadeIn();               
           }
           
           savecustomer();
           $('#error_save_customer').fadeOut();
       }
   });
   $('.delete_order').live('click',function(){
      var detail_id = $(this).attr('detail_id');
      var order_id = $('#order_id').val();
      if(confirm("Xóa sản phẩm này ?")==true){
          $.ajax({
            url: 'process/customer.php',
            type: "POST",
            async: true,
            dataType: 'json',
            data: {
                action : "delete_detail_order",detail_id:detail_id
            },
            success: function(response) {                                  
                $.ajax({
                    url: 'view/customer/list_order_detail.php',
                    type: "POST",
                    async: true,                
                    data: {
                        order_id:order_id                   
                    },
                    success: function(data) {                  
                        $('#list_order_detail').html(data);
                        scrollToListOrder();
                    }
                });
            }
        });
      }else{
          return false;
      }
   });
   $('.delete_customer').click(function(){   
       var obj = $(this);
      var customer_id = obj.attr('customer_id');
      if(confirm("Xóa khách hàng này ?")==true){
          $.ajax({
            url: 'process/customer.php',
            type: "POST",
            async: true,
            dataType: 'json',
            data: {
                action : "delete_customer",customer_id:customer_id
            },
            success: function(response) {                
                obj.parent().parent().remove();
            }
        });
      }else{
          return false;
      }
   });
   $('.delete_orders').click(function(){   
       var obj = $(this);
      var order_id = obj.attr('order_id');
      if(confirm("Xóa đơn hàng này ?")==true){
          $.ajax({
            url: 'process/customer.php',
            type: "POST",
            async: true,
            dataType: 'json',
            data: {
                action : "delete_orders",order_id:order_id
            },
            success: function(response) {                 
                obj.parent().parent().remove();
            }
        });
      }else{
          return false;
      }
   });
    $('#btnSaveOrder').click(function(){
       if($.trim($('#order_no').val())=='' || $.trim($('#date_giao').val())==''){
           $('#error_save_order').fadeIn();
           scrollToOrder();
           return false;
       }else{
           $('.alert').fadeOut();           
           saveOrder();           
           setTimeout('scrollToOrder()',1000);
           setTimeout(function(){
               $('#btnDetailOrder').click();
           },2000);
       }
   });
   $('#btnSaveEditOrder').live('click',function(){
       if($.trim($('#order_no').val())=='' || $.trim($('#total_product').val())=='' || $.trim($('#total_price').val())=='' || $.trim($('#date_giao').val())==''){
           $('#error_save_order').fadeIn();
           scrollToOrder();
           return false;
       }else{
           $('.alert').fadeOut();           
           saveOrder();                       
       }
   });
   $('#upload_images').ajaxForm({
        beforeSend: function() {
        },
        uploadProgress: function(event, position, total, percentComplete) {

        },
        complete: function(data) {
            var arrRes = JSON.parse(data.responseText);                
            $("#hinhanh").html(arrRes['text'] + arrRes['str_hinhanh']);
            $("#div_upload").dialog('close');
        }
    }); 
   $('#btnSaveOrderDetail').click(function(){
       if($.trim($('#product_code').val())=='' || $.trim($('#number_product').val())=='' || $.trim($('#price').val())=='' || $.trim($('#total_prices').val())==''){
           $('#error_save_order_detail').fadeIn();           
           return false;
       }else{
           $('.alert').fadeOut();           
           saveOrderDetail();                      
       }
   });
   $('#btnSaveSoDo').click(function(){
        checkAndSaveSoDo();
    });
    $('.detail_order').click(function(){
       var order_id = $(this).attr('order_id');
       $('#order_id').val(order_id);
       $('#btnDetailOrder_'+order_id).click();
       setTimeout('loadOrderDetail('+order_id+')',1000);
    });
    $('.detail_customer').click(function(){
       var customer_id = $(this).attr('customer_id');
       $('#customer_id').val(customer_id);
       $('#btnDetailCustomer_'+customer_id).click();
       setTimeout('loadCustomerDetail('+customer_id+')',1000);
    });
    $('.edit_order').click(function(){
       var order_id = $(this).attr('order_id');
       $('#order_id').val(order_id);
       $('#btnEditOrder_'+order_id).click();
       setTimeout('loadOrderDetailToEdit('+order_id+')',1000);
    });
    $('.edit_customer').click(function(){
       var customer_id = $(this).attr('customer_id');
       $('#customer_id').val(customer_id);
       $('#btnEditCustomer_'+customer_id).click();
       setTimeout('loadCustomerDetailToEdit('+customer_id+')',1000);
    });
   
});

function saveOrderDetail(){
    var order_id = $('#order_id').val();
    var product_code = $('#product_code').val();
    var number_product = $('#number_product').val();
    var price = $('#price').val();    
    var total_price = $('#total_prices').val();
    var mau_id = $('#mau_id').val();
    var kieu_id = $('#kieu_id').val();
    var size_id = $('#size_id').val();    
    var url_images = $('#url_images').val();    
    $.ajax({
        url: 'process/customer.php',
        type: "POST",
        async: true,
        dataType: 'json',
        data: {
            action : "save_order_detail",order_id:order_id,
            product_code: product_code,number_product:number_product,price:price,
            total_price : total_price,mau_id:mau_id,
            kieu_id : kieu_id,size_id:size_id,
            url_images : url_images
        },
        success: function(response) {                  
            $('#product_code, #number_product, #price, #total_prices').val('');
            $('#hinhanh').html('');
            $.ajax({
                url: 'view/customer/list_order_detail.php',
                type: "POST",
                async: true,                
                data: {
                    order_id:order_id                   
                },
                success: function(data) {                  
                    $('#list_order_detail').html(data);
                    scrollToListOrder();
                }
            });
        }
    });
}    
function loadOrderDetail(order_id){
    $.ajax({
        url: 'view/customer/list_order_detail.php',
        type: "POST",
        async: true,                
        data: {
            order_id:order_id                   
        },
        success: function(data) {                  
            $('#list_order_detail').html(data);
            scrollToListOrder();
        }
    });
}
function loadCustomerDetail(customer_id){
    $.ajax({
        url: 'view/customer/list_customer_detail.php',
        type: "POST",
        async: true,                
        data: {
            customer_id:customer_id                   
        },
        success: function(data) {                  
            $('#list_customer_detail').html(data);           
        }
    });
}
function loadOrderDetailToEdit(order_id){
    $.ajax({
        url: 'view/order/order_detail.php',
        type: "POST",
        async: true,                
        data: {
            order_id:order_id                   
        },
        success: function(data) {                  
            $('#list_order_detail_edit').html(data);            
        }
    });
}
function loadCustomerDetailToEdit(customer_id){
    $.ajax({
        url: 'view/customer/popup_edit_customer.php',
        type: "POST",
        async: true,                
        data: {
            customer_id:customer_id                   
        },
        success: function(data) {                  
            $('#popup_edit_customer').html(data);            
        }
    });
}
function saveOrder(){
    var action = "save_order";
    var order_id = $('#order_id').val();
    var customer_id = $('#customer_id').val();
    var order_no = $('#order_no').val();
    var total_product = $('#total_product').val();
    var total_price = $('#total_price').val();    
    var payment_id = $('#payment_id').val();
    var method_id = $('#method_id').val();
    var date_giao = $('#date_giao').val();
    var date_tra = $('#date_tra').val();    
    var description = $('#description').val();
    var order_status = $('[name="order_status"]:checked').val();
   if(order_id > 0 ) action = "edit_order";
    $.ajax({
        url: 'process/customer.php',
        type: "POST",
        async: true,
        dataType: 'json',
        data: {
            action : action,customer_id:customer_id,order_id:order_id,
            order_no: order_no,method_id:method_id,date_tra:date_tra,
            total_product : total_product,date_giao:date_giao,
            total_price : total_price,description:description,
            payment_id : payment_id,order_status:order_status
        },
        success: function(response) {
            $('#success_order').fadeIn();
            if(action=="save_order"){
                $('#order_id').val(response.order_id);                            
                $('#btnSaveOrder').hide();
                $('#btnDetailOrder').show();
                $('#order_no, #total_product, #total_price, #date_giao, #date_tra,#description').val('');
            }
        }
    });
}

function savecustomer(){
    var action = "save_customer";
    var customer_name = $('#customer_name').val();
    var phone = $('#phone').val();
    var address = $('#address').val();   
    var email = $('#email').val();  
    var information = $('#information').val();
    var customer_id = $('#customer_id').val();
    if(customer_id > 0 ) action = "edit_customer"; 
    $.ajax({
        url: 'process/customer.php',
        type: "POST",
        async: true,
        dataType: 'json',
        data: {
            action : action,
            customer_id : customer_id,
            customer_name: customer_name,
            phone : phone,email : email,
            address : address,
            information : information
        },
        success: function(response) {
            $('#success_customer').fadeIn();            
            if(action == "save_customer"){
                $('#customer_id').val(response.customer_id);            
                $('#btnSaveCustomer, #btnSaveAndInputOrder').hide();
                $('#order_information').show();
                $('#customer_name, #phone, #address, #information, #email').val('');
            }
            checkAndSaveSoDo();
        }
    });
}
function luusodo(){
    var vong_nguc = $('#vong_nguc').val();var vong_eo = $('#vong_eo').val();var vong_mong = $('#vong_mong').val();
    var ha_nguc = $('#ha_nguc').val();var ha_eo = $('#ha_eo').val();var rong_vai = $('#rong_vai').val();
    var dai_tay_ao = $('#dai_tay_ao').val();var dai_ao = $('#dai_ao').val();var vong_co = $('#vong_co').val();
    var vong_nach = $('#vong_nach').val();var bap_tay_tren = $('#bap_tay_tren').val();var bap_tay_duoi = $('#bap_tay_duoi').val();
    var vong_day = $('#vong_day').val();var vong_nguc_tren = $('#vong_nguc_tren').val(); var vong_nguc_duoi = $('#vong_nguc_duoi').val();
    var dai_quan = $('#dai_quan').val();
    var customer_id = $('#customer_id').val();
    if(customer_id > 0){
        $.ajax({
            url: 'process/customer.php',
            type: "POST",
            async: true,
            dataType: 'json',
            data: {
                action : "save_sodo",
                customer_id: customer_id,
                vong_nguc : vong_nguc, vong_eo : vong_eo,vong_mong : vong_mong,
                ha_nguc : ha_nguc, ha_eo : ha_eo,rong_vai : rong_vai,
                dai_tay_ao : dai_tay_ao, dai_ao : dai_ao,vong_co : vong_co,
                vong_nach : vong_nach, bap_tay_tren : bap_tay_tren,bap_tay_duoi : bap_tay_duoi,
                vong_day : vong_day, vong_nguc_tren : vong_nguc_tren,vong_nguc_duoi : vong_nguc_duoi,
                dai_quan : dai_quan
            },
            success: function(response) {
                if(response.result==true) {
                    $('#btnCloseSoDo').click();
                    setTimeout('scrollToOrder()',2000);
                    $('#warning_sodo').hide();
                    $('#btnSaveOrderAndDetail').show();
                }else{ 
                    $('#error_sodo_luu').fadeIn(); 
                    return false;
                }                
            }
        });
    }else{
        $('#btnCloseSoDo').click();
    }
    
}
function scrollToTop(){
   $('html, body').animate({
        scrollTop: $(".breadcrumb").offset().top
    });
}
function scrollToOrder(){
   $('html, body').animate({
        scrollTop: $("#action_customer").offset().top
    });
}
function scrollToListOrder(){
   $('#myModal3').animate({
        scrollTop: $("#list_order_detail").offset().top
    });
}
function scrollToOrderDetail(){
   $('html, body').animate({
        scrollTop: $("#btnDetailOrder").offset().top
    });
}
function scrollTopopup_edit_customer(){
   $('html, body').animate({
        scrollTop: $("#popup_edit_customers").offset().top
    });
}
function checkAndSaveSoDo(){
    var countPopup = 0;
    $('#myModal1 input.input-small').each(function(){
        if($(this).val()=='') countPopup++;               
    });   
    if(countPopup==16){
        $('#error_sodo').fadeIn();
        return false;
    }else{
        luusodo();
    }
}



